package com.tc.property.config.handler;

import com.tc.property.PropertyHolder;
import com.tc.property.config.model.ConfigContext;
import com.tc.property.util.Utils;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Properties;

public class DefaultContextChangedHandler implements IConfigChangedHandler {

	@Setter
	private List<PropertyChangedHandler> handlers;

	@Override
	public void handle(ConfigContext previous, ConfigContext current) {
		Properties newProps = Utils.getPropsFromContext(current);
		PropertyHolder.addProperties(newProps);

		if (CollectionUtils.isNotEmpty(handlers)) {
			for (PropertyChangedHandler handler : handlers) {
				handler.execute(Utils.getPropsFromContext(previous), newProps);
			}
		}

	}

}
